package com.huiren.ui.importUI;

import javax.annotation.PostConstruct;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.huiren.bean.Goods;
import com.huiren.bean.Supplier;
import com.huiren.bean.Warehouse;
import com.huiren.service.GoodsService;
import com.huiren.service.SupplierService;
import com.huiren.service.WarehouseService;
import com.huiren.util.JdbcUtil;
import com.huiren.util.XMLUtil;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;

@Component
public class XMLExportJPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(XMLExportJPanel.class);

	@Autowired
	private GoodsService goodsService;
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private WarehouseService warehouseService;

	public XMLExportJPanel() {

	}

	@PostConstruct
	public void initMethod() {

		JButton btnNewButton = new JButton("供应商导出");
		add(btnNewButton);

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// 选择文件 按钮
				JFileChooser c = new JFileChooser();
				c.setDialogTitle("保存");
				c.setFileSelectionMode(JFileChooser.SAVE_DIALOG
						| JFileChooser.DIRECTORIES_ONLY);
				c.setApproveButtonText("保存");

				int rVal = c.showOpenDialog(XMLExportJPanel.this);
				if (rVal == JFileChooser.APPROVE_OPTION) {
					List<Map<String, Object>> list = JdbcUtil
							.executeQuery("select * from T_YJ_CONFIG where key='selectsupplier'");
					String sql = list.get(0).get("value").toString();
					try {
						logger.info("供应商导出");
						List<Supplier> slist = supplierService
								.selectSupplier(sql);
						Document document = XMLUtil.createXML(slist, "SUPPS",
								"SUPP");
						Writer fileWriter = new FileWriter(c.getSelectedFile()
								.getAbsolutePath() + File.separator + "供应商.xml");
						XMLWriter xmlWriter = new XMLWriter(fileWriter);
						xmlWriter.write(document); // 写入文件中
						xmlWriter.close();
					} catch (Exception e) {
						logger.error(e);
						e.printStackTrace();
					}
				}

			}
		});

		JButton btnNewButton_1 = new JButton("药库导出");
		add(btnNewButton_1);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// 选择文件 按钮
				JFileChooser c = new JFileChooser();
				c.setDialogTitle("保存");
				c.setFileSelectionMode(JFileChooser.SAVE_DIALOG
						| JFileChooser.DIRECTORIES_ONLY);
				c.setApproveButtonText("保存");
				int rVal = c.showOpenDialog(XMLExportJPanel.this);
				if (rVal == JFileChooser.APPROVE_OPTION) {
					List<Map<String, Object>> list = JdbcUtil
							.executeQuery("select * from T_YJ_CONFIG where key='selectwarehouse'");
					String sql = list.get(0).get("value").toString();
					try {
						logger.info("药库导出");
						List<Warehouse> wlist = warehouseService
								.selectWarehouse(sql);
						Document document = XMLUtil.createXML(wlist,
								"WAREHOUSES", "WAREHOUSE");
						Writer fileWriter = new FileWriter(c.getSelectedFile()
								.getAbsolutePath() + File.separator + "药库.xml");
						XMLWriter xmlWriter = new XMLWriter(fileWriter);
						xmlWriter.write(document); // 写入文件中
						xmlWriter.close();
					} catch (Exception e) {
						logger.error(e);
						e.printStackTrace();
					}

				}
			}
		});

		JButton btnNewButton_2 = new JButton("药品导出");
		add(btnNewButton_2);
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// 选择文件 按钮
				JFileChooser c = new JFileChooser();
				c.setDialogTitle("保存");
				c.setFileSelectionMode(JFileChooser.SAVE_DIALOG
						| JFileChooser.DIRECTORIES_ONLY);
				c.setApproveButtonText("保存");
				int rVal = c.showOpenDialog(XMLExportJPanel.this);
				if (rVal == JFileChooser.APPROVE_OPTION) {
					List<Map<String, Object>> list = JdbcUtil
							.executeQuery("select * from T_YJ_CONFIG where key='selectgoods'");
					String sql = list.get(0).get("value").toString();
					try {
						logger.info("药品导出");
						List<Goods> glist = goodsService.selectGoods(sql);
						Document document = XMLUtil.createXML(glist, "GOODS",
								"GOOD");
						Writer fileWriter = new FileWriter(c.getSelectedFile()
								.getAbsolutePath() + File.separator + "药品.xml");
						XMLWriter xmlWriter = new XMLWriter(fileWriter);
						xmlWriter.write(document); // 写入文件中
						xmlWriter.close();
					} catch (Exception e) {
						logger.error(e);
						e.printStackTrace();
					}

				}
			}
		});
	}
}
